package com.burgeon.backend.SHOP.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Consultant;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindConsultantAll extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public FindConsultantAll(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Consultant> getData(String name) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(name), url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)) {
            throw new Exception(message);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        if ("null".equals(jsonArray.getJSONArray(0).getString(0))){
            return null;
        }
        ArrayList<Consultant> consultantList = new ArrayList<>();
        for (int i = 0; i <jsonArray.length() ; i++) {
            Consultant consultant = new Consultant();
            consultant.setId(Integer.parseInt(jsonArray.getJSONArray(i).getString(0)));
            consultant.setName(jsonArray.getJSONArray(i).getString(1));
            consultant.setPhone(jsonArray.getJSONArray(i).getString(2));
            consultant.setStore(jsonArray.getJSONArray(i).getString(3));
            consultant.setSex(jsonArray.getJSONArray(i).getString(4));
            consultantList.add(consultant);
        }

        return consultantList;
    }

    private MultiValueMap Data(String name){
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("NAME");
        columns.add("PHONE");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("SEX");

        params.put("columns",columns);
        params.put("table","C_V_EMPLOYEE");

        params.put("count", "true");
        params.put("range",500);

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine","and");
        expr1v1.put("column","'C_STORE_ID;ISRETAIL'");
        expr1v1.put("condition","'Y'");
        expr1.put("expr1",expr1v1);
        expr1v2.put("column","'C_STORE_ID;EMPCNT'");
        expr1v2.put("condition","is not null");
        expr1.put("expr2",expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine","and");
        expr2v1.put("column","'C_STORE_ID;ISACTIVE'");
        expr2v1.put("condition","Y");
        expr2.put("expr1",expr2v1);
        expr2v2.put("column","NAME");
        expr2v2.put("condition","'"+name+"'");
        expr2.put("expr2",expr2v2);

        paramss.put("combine", "and");//关系
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params",paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindConsultantAll g = new FindConsultantAll("39.104.240.134:55321","nea@burgeon.com.cn","root55321");
        try {
            System.err.println(g.getData(""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
